package com.canbot.u05.activity.agenda.calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

import com.canbot.u05.R;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MultiMonthView;

/**
 * @author yinxiaowei
 * @date 2019/5/31
 */
public class CustomMonthView extends MultiMonthView {
    private int pading ;
    private int mRadius;
    private Paint curentTextPaint;
    private float textHeight;

    public CustomMonthView(Context context) {
        super(context);
        pading = (int) context.getResources().getDimension(R.dimen.px11);
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 2;
        mSchemePaint.setStyle(Paint.Style.STROKE);

        curentTextPaint = new Paint();
        curentTextPaint.setAntiAlias(true);
        curentTextPaint.setTextAlign(Paint.Align.CENTER);
        curentTextPaint.setColor(getContext().getColor(R.color.blue));
        curentTextPaint.setFakeBoldText(true);
        curentTextPaint.setTextSize(14);
        Paint.FontMetrics fontMetrics = curentTextPaint.getFontMetrics();
        textHeight = fontMetrics.descent - fontMetrics.ascent;
    }

    private static final String TAG = "CustomMultiMonthView";

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme,
                                     boolean isSelectedPre, boolean isSelectedNext) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        mSelectedPaint.setColor(getResources().getColor(R.color.blue, null));
        Log.e(TAG, "onDrawSelected: " + x + "  " + y + "  " + mItemWidth + "  " + mItemHeight);
//        canvas.drawRect(x, y, x + mItemWidth, y + mItemHeight, mSelectedPaint);

        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
//        canvas.drawRoundRect(new RectF(x + pading, y + pading, x + mItemWidth - pading, y + mItemHeight - pading), 20, 20, mSelectedPaint);
        return false;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y, boolean isSelected) {
        Log.e(TAG, "onDrawScheme: " + calendar + "  " + calendar.getWeek());
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        mSchemePaint.setColor(Color.GRAY);
        mSchemePaint.setStyle(Paint.Style.FILL);

        canvas.drawCircle(cx, y + pading, mRadius / 6, mSchemePaint);
//        canvas.drawRect(x, y, mItemWidth, mItemHeight, mSelectedPaint);
    }

    /**
     * z
     *
     * @param canvas     canvas
     * @param calendar   日历calendar
     * @param x          日历Card x起点坐标
     * @param y          日历Card y起点坐标
     * @param hasScheme  是否是标记的日期
     * @param isSelected 是否选中
     */
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        Log.e(TAG, "onDrawText: " + calendar + "   " + isSelected + "   " + hasScheme);
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;

        boolean isInRange = isInRange(calendar);
        boolean isEnable = !onCalendarIntercept(calendar);
        boolean currentDay = calendar.isCurrentDay();

        if (currentDay) {
            int cy = y + mItemHeight / 2;
            canvas.drawText(getContext().getString(R.string.today), cx, cy + mRadius + textHeight / 2 - pading, curentTextPaint);
        }
        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    mSelectTextPaint);
        } else if (hasScheme) {
            onDrawScheme(canvas, calendar, x, y, false);
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    calendar.isCurrentMonth() && isInRange && isEnable ? mCurMonthTextPaint : mOtherMonthTextPaint);
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    currentDay ? mCurDayTextPaint :
                            calendar.isCurrentMonth() && isInRange && isEnable ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }
}
